package com.mathum.baseapp.widget;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.mathum.baseapp.R;
import com.mathum.baseapp.adapter.BaseDialogAdapter;
import com.mathum.baseapp.callback.OnItemClickListener;
import com.mathum.baseapp.data.BaseDialogData;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * RecyclerView式样的对话框
 * <p>
 * Created by 43053 on 2017/1/6.
 */

public class RecyclerViewDialog extends DialogFragment {

    public OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public static RecyclerViewDialog getInstance(List<BaseDialogData> dialogItemList, int resId, String title) {

        RecyclerViewDialog recyclerViewDialog = new RecyclerViewDialog();

        Bundle bundle = new Bundle();
        bundle.putSerializable("list", (Serializable) dialogItemList);
        bundle.putInt("resId", resId);
        bundle.putString("title", title);
        recyclerViewDialog.setArguments(bundle);
        return recyclerViewDialog;
    }

    public static RecyclerViewDialog getInstance(List<BaseDialogData> dialogItemList) {

        return getInstance(dialogItemList, 0, null);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        RecyclerView recyclerView;
        BaseDialogAdapter baseDialogAdapter;
        Bundle bundle = getArguments();
        List<BaseDialogData> dialogDataList;
        int resId = 0;
        String title = null;
        if (bundle != null) {
            dialogDataList = (List<BaseDialogData>) bundle.getSerializable("list");
            resId = bundle.getInt("resId");
            title = bundle.getString("title");
        } else {
            dialogDataList = new ArrayList<>();
        }
        if (resId == 0) {
            resId = R.layout.item_recyclerview_dialog_rv;
        }
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_recyclerview, null, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        baseDialogAdapter = new BaseDialogAdapter(dialogDataList, resId);
        baseDialogAdapter.setOnItemClickListener((position, view1) -> {
            if (onItemClickListener != null){
                onItemClickListener.onItemClick(position, view1);
            }
        });
        recyclerView.setAdapter(baseDialogAdapter);

        return new AlertDialog.Builder(getActivity())
                .setTitle(title)
                .setView(view)
                .create();
    }
}
